<?php
define('FUNC_IN', 'Function in');
define('FUNC_OUT', 'Function out');
define('VLIB_INSERT', 'Insert data');
define('VLIB_UPDATE', 'Update data');
define('VLIB_DELETE', 'Delete data');
define('VLIB_SELECT', 'Select data');

class Vlib_Logger {

    static $_instance = null;

    public function __construct() {
        
    }
    
    /**------------------------------------------------------------------------------------
     * @desc instance
     * 
     * @author Tuyendn<tuyendn@vnext.vn>
     * @since 2012/02/23
     *------------------------------------------------------------------------------------*/
    public static function getInstance() {
        if (self::$_instance == null) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**------------------------------------------------------------------------------------
     * @desc write 'function in' into log file
     * 
     * @author Minhdq<minhdq@vnext.vn>
     * @since 2012/02/23
     *------------------------------------------------------------------------------------*/
    public function debugIn() {
        //$_arrTrace = debug_backtrace();
        //$this->writeDebugLog($_arrTrace, FUNC_IN);
    }
    
    /**------------------------------------------------------------------------------------
     * @desc write 'function out' into log file
     * 
     * @author Minhdq<minhdq@vnext.vn>
     * @since 2012/02/23
     *------------------------------------------------------------------------------------*/
    public function debugOut() {
        //$_arrTrace = debug_backtrace();
        //$this->writeDebugLog($_arrTrace, FUNC_OUT);
    }
    
    /**------------------------------------------------------------------------------------
     * @desc write Error into log file
     * 
     * @author Minhdq<minhdq@vnext.vn>
     * @since 2012/02/23
     *------------------------------------------------------------------------------------*/
    public function error($errorMsg, $title=VLIB_SELECT) {
        $tmp = debug_backtrace();
        $result = array();
        $result['class'] = $tmp[2]['class'];
        $result['function'] = $tmp[2]['function'];
        $result['file'] = $tmp[1]['file'];
        $result['line'] = $tmp[1]['line'];
        $logData = '[' . $result['class'] . '::' . $result['function'] . '()] [ Error message = ' . $errorMsg . ']';
        //$this->write(Zend_Log::ERR, $title, $logData, $result['file'], $result['line']);
    }
    
    /**------------------------------------------------------------------------------------
     * @desc write SQL into log file
     * 
     * @author Minhdq<minhdq@vnext.vn>
     * @since 2012/02/23
     *------------------------------------------------------------------------------------*/
    public function sql($sql, $table = null, $aryBin = null, $sqltitle = VLIB_SELECT) {
        $_arrTrace = debug_backtrace();
        $_function = $_arrTrace[1]['function'];
        $_file = $_arrTrace[0]['file'];
        $_line = $_arrTrace[0]['line'];
        $logData = '';

        if ($table != null)
            $logData .= '[' . $nameTable . ' Success]';

        $logData .= '[SQL of ' . $_function . ': ' . $sql . ']';

        if ($aryBin != null) {
            $logData .= "[Array bin: \n " . print_r($aryBin, true) . ' ]';
        } else {
            $logData .= '[Array bin not use ]';
        }

        //$this->write(Zend_Log::DEBUG, $sqltitle, $logData, $_file, $_line);
    }
    
    /**------------------------------------------------------------------------------------
     * @desc write debug into log file
     * 
     * @author Minhdq<minhdq@vnext.vn>
     * @since 2012/02/23
     *------------------------------------------------------------------------------------*/
    private function writeDebugLog($_arrTrace, $logTitle) {
        $_function = $_arrTrace[1]['function'];
        $_class = $_arrTrace[1]['class'];
        $_file = $_arrTrace[0]['file'];
        $_line = $_arrTrace[0]['line'];

        $logData = "[" . $_class . "::" . $_function . "()]";
        //$this->write(Zend_Log::DEBUG, $logTitle, $logData, $_file, $_line);
    }
    
    /**------------------------------------------------------------------------------------
     * @desc write file
     * 
     * @author Minhdq<minhdq@vnext.vn>
     * @since 2012/02/23
     *------------------------------------------------------------------------------------*/
    public function write($type, $title, $data, $file, $line){
        $logFormat =  '%message%'. PHP_EOL;
        
        $dateFile = date('Ymd');
        $config = Vlib_Config::getConfig();
        $logFile = $config['logger']['log_path']."".$dateFile."_".$config['logger']['log_name'];
        $stream = @fopen($logFile, 'a', false);
        if (! $stream) {
            $stream = @fopen($logFile, 'w', false);
        }
        $typeName = '';
        switch ($type){
            case Zend_Log::DEBUG:
                $typeName = "Debug";
                break;
            case Zend_Log::ERR:
                $typeName = "Error";
                break;
            case Zend_Log::INFO:
                $typeName = "Action";
                break;
        }
        $flag = false;
        if ($config['logger']['level'] == 1) {
            switch ($type) {
                case Zend_Log::ERR:
                    $flag = true;
                    break;
            }
        } elseif ($config['logger']['level'] == 2) {
            switch ($type) {
                case Zend_Log::ERR:
                case Zend_Log::INFO:
                    $flag = true;
                    break;
            }
        } elseif ($config['logger']['level'] == 3) {
            switch ($type) {
                case Zend_Log::ERR:
                case Zend_Log::INFO:
                case Zend_Log::DEBUG:
                    $flag = true;
                    break;
            }
        }
        if ($flag) {
            $logWriter = new Zend_Log_Writer_Stream($stream);
            $logWriter->setFormatter(New Zend_Log_Formatter_Simple($logFormat));

            $logController = new Zend_Log($logWriter);
            $time = date('Y-m-d H:i:s');
            $serverAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR']: 'CLI_MODE';
            $message = "[".$time."] [".$serverAddress."] [".$typeName."] [".$title."] ".$data." [".$file."] [LINE:".$line."]";
            $logController->log($message, $type); // Type : 0-7
        }
    }
}